﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace ClientDll
{
    enum OperationClass { DELETE, APPEND };
    struct Point
    {
        public int idx, x, y, path;
    }
    struct Operation
    {
        public OperationClass opc;
        public int idx, x, y, path;
    }



    public class DrawData
    {
        List<Point> Points;
        int lastOperation = 0;
        public DrawData()
        {
            Points = new List<Point>();
        }

        void parseOperation(StreamReader s)
        {
            string line;
            while ((line = s.ReadLine()) != null)
            {
                parseOperation(line);
            }
        }
        void parseOperation(string s)
        {
            string[] tmp = s.Split('|');
            if (tmp.Length != 5) return;
            Operation op = new Operation();
            if (tmp[0] == "DELETE")
            {
                op.opc = OperationClass.DELETE;
            }
            else
            {
                if (tmp[0] == "APPEND")
                {
                    op.opc = OperationClass.APPEND;
                }
                else return;
            }
            op.idx = Convert.ToInt32(tmp[1]);
            op.x = Convert.ToInt32(tmp[2]);
            op.y = Convert.ToInt32(tmp[3]);
            op.path = Convert.ToInt32(tmp[4]);
            processOperation(op);
        }

        void processOperation(Operation op)
        {
            if (op.opc == OperationClass.DELETE)
            {
                foreach (Point p in Points)
                {
                    if (p.idx == op.idx)
                    {
                        Points.Remove(p);
                    }
                }
            }
            else
            {
                Point p = new Point();
                p.idx = op.idx;
                p.x = op.x;
                p.y = op.y;
                p.path = op.path;
                Points.Add(p);
            }
        }
        public void update(string baseUrl, Boolean clean)
        {
            if (clean)
            {
                Points.Clear();
                update(baseUrl);
            }
            else
                update(baseUrl);
        }
        void update(string baseUrl)
        {
            string sUrl = "";
            //sUrl = "127.0.0.1/WOTDraw/outdata.php?num=";
            sUrl = baseUrl;
            sUrl = sUrl + Convert.ToString(lastOperation);
            StreamReader s = Http_GetResult(sUrl);
            parseOperation(s);
        }


        private StreamReader Http_GetResult(string url)
        {
            WebRequest wrGETURL;
            wrGETURL = WebRequest.Create(url);
            wrGETURL.Proxy = WebProxy.GetDefaultProxy();
            Stream objStream;
            objStream = wrGETURL.GetResponse().GetResponseStream();
            StreamReader objReader = new StreamReader(objStream);
            return objReader;
        }
    }
}
